<article class="" id="post-">
<header class="entry-header">
<h1 class="entry-title">.get()</h1>
<div class="entry-meta">
						所属分类：<span class="category"><a href="/category/miscellaneous/">杂项</a> &gt; <a href="/category/miscellaneous/dom-element-methods/">DOM 元素方法</a></span>
<span class="pull-right">英文文档：<a href="https://api.jquery123.com/get/" target="_blank">.get()</a></span>
</div><!-- .entry-meta -->
</header><!-- .entry-header -->
<!-- .entry-header -->
<div class="entry-content">
			  通过检索匹配jQuery对象得到对应的DOM元素。
			  <div class="toc">
<h4><span>Contents:</span></h4>
<ul class="toc-list">
<li>
<a href="#get1">.get( index )</a><ul><li><a href="#get-index">.get( index )</a></li></ul>
</li>
<li>
<a href="#get2">.get()</a><ul><li><a href="#get">.get()</a></li></ul>
</li>
</ul>
</div>
<article class="entry method" id="get1"><h2 class="section-title">
<span class="name">.get( index )</span><span class="returns">返回: <a href="/Types/#Element,%20Array">Element, Array</a></span>
</h2>
<div class="entry-wrapper">
<p class="desc"><strong>描述: </strong>通过jQuery对象获取一个对应的DOM元素。</p>
<ul class="signatures"><li class="signature" id="get-index">
<h4 class="name">
<span class="version-details">添加的版本: <a href="/category/version/1.0/">1.0</a></span>.get(  [index ] )</h4>
<ul><li>
<div><strong>index</strong></div>
<div>类型: <a href="/Types/#Number">Number</a>
</div>
<div>从0开始计数，用来确定获取哪个元素。</div>
</li></ul>
</li></ul>
<div class="longdesc" id="entry-longdesc">
<p><code>.get()</code> 方法允许我们直接访问jQuery对象中隐含的DOM节点。假设我们页面上有一个简单的无序列表：</p>
<div class="syntaxhighlighter xml nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">ul</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">li</span> <span class="attribute">id</span>=<span class="value">"foo"</span>&gt;</span>foo<span class="tag">&lt;/<span class="title">li</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">li</span> <span class="attribute">id</span>=<span class="value">"bar"</span>&gt;</span>bar<span class="tag">&lt;/<span class="title">li</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">ul</span>&gt;</span></code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>如果指定了 index 参数， .get() 则会获取单个元素：</p>
<div class="syntaxhighlighter javascript nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>console.log( $( <span class="string">"li"</span> ).get( <span class="number">0</span> ) );</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>由于索引 index 是以 0 开始计数的，所以上面代码返回了第一个列表项：</p>
<p>
<span class="output">&lt;li id="foo"&gt;</span>
</p>
<p>每个 jQuery 对象也被当成一个数组，所以我们也可以直接用数组的取值运算符来获得列表项：</p>
<div class="syntaxhighlighter javascript nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>console.log( $( <span class="string">"li"</span> )[<span class="number">0</span>] );</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>然而，这种语法缺少某些 .get() 所具有的附加功能，比如可以指定索引值为负值：</p>
<div class="syntaxhighlighter javascript nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>console.log( $( <span class="string">"li"</span> ).get(-<span class="number">1</span>) );</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>负的索引值表示从匹配的集合中从末尾开始倒数，所以上面这个例子将会返回列表中最后一项：</p>
<p>
<span class="output">&lt;li id="bar"&gt;</span>
</p>
</div>
<section class="entry-examples" id="entry-examples">
<header><h2 class="underline">例子:</h2></header>
<div class="entry-example" id="example-1">
<h4>Example: <span class="desc">给出点中元素的标签名。</span>
</h4>
<div class="syntaxhighlighter xml ">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
<div class="line n5">5</div>
<div class="line n6">6</div>
<div class="line n7">7</div>
<div class="line n8">8</div>
<div class="line n9">9</div>
<div class="line n10">10</div>
<div class="line n11">11</div>
<div class="line n12">12</div>
<div class="line n13">13</div>
<div class="line n14">14</div>
<div class="line n15">15</div>
<div class="line n16">16</div>
<div class="line n17">17</div>
<div class="line n18">18</div>
<div class="line n19">19</div>
<div class="line n20">20</div>
<div class="line n21">21</div>
<div class="line n22">22</div>
<div class="line n23">23</div>
<div class="line n24">24</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code><span class="doctype">&lt;!DOCTYPE html&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">html</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">head</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">style</span>&gt;</span><span class="css"></span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">span</span> <span class="rules">{ <span class="rule"><span class="attribute">color</span>:<span class="value">red;</span></span> <span class="rule">}</span></span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">div</span> <span class="rules">{ <span class="rule"><span class="attribute">background</span>:<span class="value">yellow;</span></span> <span class="rule">}</span></span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;/<span class="title">style</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">script</span> <span class="attribute">src</span>=<span class="value">"https://code.jquery.com/jquery-latest.js"</span>&gt;</span><span class="javascript"></span><span class="tag">&lt;/<span class="title">script</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">head</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">body</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">span</span>&gt;</span>&amp;nbsp;<span class="tag">&lt;/<span class="title">span</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">p</span>&gt;</span>In this paragraph is an <span class="tag">&lt;<span class="title">span</span>&gt;</span>important<span class="tag">&lt;/<span class="title">span</span>&gt;</span> section<span class="tag">&lt;/<span class="title">p</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">div</span>&gt;</span><span class="tag">&lt;<span class="title">input</span> <span class="attribute">type</span>=<span class="value">"text"</span> /&gt;</span><span class="tag">&lt;/<span class="title">div</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">script</span>&gt;</span><span class="javascript"></span></code></div></div><div class="container"><div class="line"><code>$(<span class="string">"*"</span>, document.body).click(<span class="function"><span class="keyword">function</span> <span class="params">(e)</span> {</span></code></div></div><div class="container"><div class="line"><code>  e.stopPropagation();</code></div></div><div class="container"><div class="line"><code>  <span class="keyword">var</span> domEl = $(<span class="keyword">this</span>).get(<span class="number">0</span>);</code></div></div><div class="container"><div class="line"><code>  $(<span class="string">"span:first"</span>).text(<span class="string">"Clicked on - "</span> + domEl.tagName);</code></div></div><div class="container"><div class="line"><code>});</code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">script</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">body</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">html</span>&gt;</span></code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<h4>Demo:</h4>
<div class="demo code-demo"></div>
</div></section>
</div></article> </div>
</article>